/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.web;

import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.Message;
import org.crystalrange.model.MenuFolder;
import org.crystalrange.model.MenuItem;
import org.crystalrange.model.Profile;
import org.crystalrange.model.User;
import org.crystalrange.service.ProfileService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Window;

public class ProfileController extends BaseController {

    protected ProfileService profileService;
    protected Profile profile;
    protected Listbox listboxUser;
    protected Listbox listboxMenuFolder;
    protected Listbox listboxMenuItem;

    public ProfileService getProfileService() {
        return this.profileService;
    }

    public void setProfileService(ProfileService profileService) {
        this.profileService = profileService;
    }

    public Profile getProfile() {
        return this.profile;
    }

    public void setProfile(Profile profile) {
        this.profile = profile;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        if (this.execution.getParameter("Id") != null) {
            this.profile = this.profileService.find(
                    new Long(this.execution.getParameter("Id")));
            this.labelTitle.setValue("Edit Profile");
        } else {
            this.profile = this.profileService.create(
                    SecurityContextHolder.getContext().getAuthentication().getName());
            this.labelTitle.setValue("Create Profile");
        }
        component.setAttribute("controller", this);
    }

    public void onClick$menuitemUserAdd(Event event) throws Exception {
        this.userAdd();
    }

    public void onClick$menuitemUserRemove(Event event) throws Exception {
        this.userRemove();
    }

    public void onClick$toolbarbuttonUserAdd(Event event) throws Exception {
        this.userAdd();
    }

    public void onClick$toolbarbuttonUserRemove(Event event) throws Exception {
        this.userRemove();
    }

    public void onClick$menuitemMenuFolderAdd(Event event) throws Exception {
        this.menuFolderAdd();
    }

    public void onClick$menuitemMenuFolderRemove(Event event) throws Exception {
        this.menuFolderRemove();
    }

    public void onClick$toolbarbuttonMenuFolderAdd(Event event) throws Exception {
        this.menuFolderAdd();
    }

    public void onClick$toolbarbuttonMenuFolderRemove(Event event) throws Exception {
        this.menuFolderRemove();
    }

    public void onClick$menuitemMenuItemAdd(Event event) throws Exception {
        this.menuItemAdd();
    }

    public void onClick$menuitemMenuItemRemove(Event event) throws Exception {
        this.menuItemRemove();
    }

    public void onClick$toolbarbuttonMenuItemAdd(Event event) throws Exception {
        this.menuItemAdd();
    }

    public void onClick$toolbarbuttonMenuItemRemove(Event event) throws Exception {
        this.menuItemRemove();
    }

    public void onClick$menuitemProfileSave(Event event) throws Exception {
        this.profileSave();
    }

    public void onClick$toolbarbuttonProfileSave(Event event) throws Exception {
        this.profileSave();
    }

    private void userAdd() throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/userSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            User selectedUser = (User) this.execution.getAttribute("selectedUser");
            this.profile.getUsers().add(selectedUser);
        }

        this.binder.loadComponent(this.listboxUser);
    }

    private void userRemove() throws Exception {
        Set<Listitem> selectedItems = this.listboxUser.getSelectedItems();

        for (Listitem selectedItem : selectedItems) {
            User selectedUser = (User) selectedItem.getValue();
            this.profile.getUsers().remove(selectedUser);
        }

        this.binder.loadComponent(this.listboxUser);
    }

    private void menuFolderAdd() throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/menuFolderSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            MenuFolder selectedMenuFolder = (MenuFolder) this.execution.getAttribute("selectedMenuFolder");
            this.profile.getMenuFolders().add(selectedMenuFolder);
        }

        this.binder.loadComponent(this.listboxMenuFolder);
    }

    private void menuFolderRemove() throws Exception {
        Set<Listitem> selectedItems = this.listboxMenuFolder.getSelectedItems();

        for (Listitem selectedItem : selectedItems) {
            MenuFolder selectedMenuFolder = (MenuFolder) selectedItem.getValue();
            this.profile.getMenuFolders().remove(selectedMenuFolder);
        }

        this.binder.loadComponent(this.listboxMenuFolder);
    }

    private void menuItemAdd() throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/menuItemSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            MenuItem selectedMenuItem = (MenuItem) this.execution.getAttribute("selectedMenuItem");
            this.profile.getMenuItems().add(selectedMenuItem);
        }

        this.binder.loadComponent(this.listboxMenuItem);
    }

    private void menuItemRemove() throws Exception {
        Set<Listitem> selectedItems = this.listboxMenuItem.getSelectedItems();

        for (Listitem selectedItem : selectedItems) {
            MenuItem selectedMenuItem = (MenuItem) selectedItem.getValue();
            this.profile.getMenuItems().remove(selectedMenuItem);
        }

        this.binder.loadComponent(this.listboxMenuItem);
    }

    private void profileSave() throws Exception {
        this.profileService.save(this.profile,
                SecurityContextHolder.getContext().getAuthentication().getName());
        this.sessionScope.put("CRYSTALRANGE_MESSAGE", new Message("Saved successfully!"));
        Clients.evalJavaScript("window.history.back();");
    }
}
